package com.example.healthapp.ui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.healthapp.R;
import com.example.healthapp.utils.InterfaceTool;
import com.example.healthapp.utils.SPManage;

// 体重页面的Fragment
public class WeightActivity extends AppCompatActivity {

    EditText weight_count;
    ImageView weight_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weight);
        InterfaceTool.setFullScreen(this, true);
        intView();
        refreshView();
        event();
    }

    private void event() {
        weight_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        weight_count.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_DONE) {
                    Toast.makeText(WeightActivity.this, "录入成功", Toast.LENGTH_SHORT).show();
                    SPManage.getSP(WeightActivity.this, "health").putString("weight", weight_count.getText().toString()).commit();
                    return true;
                }
                return false;
            }
        });
    }

    private void refreshView() {
        String weight = SPManage.getSP(this, "health").getString("weight");
        weight_count.setText(weight);
    }

    private void intView() {
        weight_count = findViewById(R.id.weight_count);
        weight_back = findViewById(R.id.weight_back);
    }
}